import { Action, Db } from 'uokay'
import database from '../database'
import axios from 'axios'

export default class GroupAction extends Action {
    add (okay, event, pipe) {
        return new Promise((resolve, reject) => {
            let name = okay.data.name
            let capacity = okay.data.capacity
            let owner = sessionStorage.getItem('id')
            axios.post('/groups/add',{
                name: name,
                capacity: capacity,
                owner: owner
            }).then(result => {
                pipe.set('result', result)
                resolve('success')
            }).catch(err => {
                pipe.set('error', err)
                resolve: owner
            })
        })
    }
    add_success (okay, event, pipe) {
        let result = pipe.get('result')
        if ( result.data.code == 2000 ) {
            return 'success'
        }else {
            alert( result.data.msg )
        }
    }
    add_fail (okay, event, pipe) {
        let error = pipe.get('error')
        alert( error )
    }
    update_group(okay, event, pipe) {
        let result = pipe.get('result')
        let db = Db.i(database)
        db.table('et_group').delete()
        for (let group of result.data.data.group) {
            db.table('et_group').insert(group)
        }
        okay.go('-1', false, 'app')
    }
}